package com.fenger.sys.service.impl;

import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.domain.Dept;
import com.fenger.sys.mapper.DeptMapper;
import com.fenger.sys.service.DeptService;
import com.fenger.sys.utils.DataGirdView;
import com.fenger.sys.vo.DeptVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DeptServiceImpl implements DeptService {
    @Autowired
    DeptMapper deptMapper;
    @Override
    @Transactional(readOnly = true)
    public List<Dept> queryAllDept(DeptVo deptVo) {
        return this.deptMapper.queryAllDept(deptVo);
    }
    @Override
    @Transactional(readOnly = true)
    public DataGirdView queryAllDeptForPage(DeptVo deptVo) {
        Page<Object> page = PageHelper.startPage(deptVo.getPage(), deptVo.getLimit());
        List<Dept> depts = this.deptMapper.queryAllDept(deptVo);

        return new DataGirdView(SysConstast.SUCCESS_CODE,SysConstast.SUCCESS_MSG,page.getTotal(),depts) ;
    }

    @Override
    public void deleteDept(Integer id) {
        this.deptMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateDept(DeptVo deptVo) {
        this.deptMapper.updateByPrimaryKeySelective(deptVo);
    }

    @Override
    public void addDept(DeptVo deptVo) {
        this.deptMapper.insertSelective(deptVo);
    }

    @Override
    public Integer queryMaxOrderNum() {
        return this.deptMapper.queryMaxOrderNum()+1;
    }

    @Override
    public Boolean checkHasChildren(Integer id) {
        Integer count = this.deptMapper.queryCountDeptByPid(id);
        return count>0;
    }
}
